package com.talent.web.utils;

import com.alibaba.excel.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @ClassName：IpUtil
 * @Author: hszhou
 * @Date: 2025/1/22 13:30
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class IpUtil {

    public static String getIpAddr(HttpServletRequest request){

        String ipAddr;
        try{
            ipAddr = request.getHeader("x-forwarded-for");
            if(StringUtils.isBlank(ipAddr) || "unknown".equalsIgnoreCase(ipAddr)){
                ipAddr  = request.getHeader("Proxy-Client-IP");
            }
            if(StringUtils.isBlank(ipAddr) || "unknown".equalsIgnoreCase(ipAddr)){
                ipAddr  = request.getHeader("WL-Proxy-Client-IP");
            }
            if(StringUtils.isBlank(ipAddr) || "unknown".equalsIgnoreCase(ipAddr)) {
                ipAddr = request.getRemoteAddr();
            }
            if(Objects.equals(ipAddr,"0:0:0:0:0:0:0:1")){
                ipAddr  ="127.0.0.1";
            }
            if(ipAddr!=null && ipAddr.length()>15 && ipAddr.indexOf(",")>0){
                ipAddr=ipAddr.substring(0,ipAddr.indexOf(","));
            }
        }catch (Exception e){

            ipAddr = "0.0.0.0";
        }

        return ipAddr;
    }
}
